package com.ivvi.autoreco;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.ivvi.autoreco.model.LocalImage;
import com.ivvi.autoreco.source.ImageDataSource;
import com.ivvi.autoreco.source.local.ImageLocalDataSource;
import com.orhanobut.logger.Logger;

import org.opencv.ivvi.custom.AutoRecognitionHelper;
import org.opencv.ivvi.custom.BitmapHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by duanxianping on 2017/5/17.
 */

public class BusinessCenter {

    private static BusinessCenter instance = new BusinessCenter();

    public static BusinessCenter getInstance() {
        return instance;
    }


    private OnBusinessListener businessListener;

    public void setBusinessListener(OnBusinessListener businessListener) {
        this.businessListener = businessListener;
    }

    public interface OnBusinessListener {
        void onStart();

        void onReco(LocalImage localImage);

        void onResult(LocalImage localImage, double[] d, long spendTime);

        void onFinish(int total, long spendTime);
    }

    private void postStartMsg() {
        if (businessListener != null) {
            businessListener.onStart();
        }
    }

    private void postResultMsg(LocalImage localImage, double[] d, long spendTime) {
        if (businessListener != null) {
            businessListener.onResult(localImage, d, spendTime);
        }
    }

    private void postFinishMsg(int total, long spendTime) {
        if (businessListener != null) {
            businessListener.onFinish(total, spendTime);
        }
    }

    private void postRecoMsg(LocalImage localImage) {
        if (businessListener != null) {
            businessListener.onReco(localImage);
        }
    }


    public void start(Context context) {

        final AutoRecognitionHelper autoRecognitionHelper = AutoRecognitionHelper.getInstance();

        postStartMsg();
        final ImageLocalDataSource localDataSource = new ImageLocalDataSource();
        localDataSource.loadLocalImages(context, new ImageDataSource.LoadImagesCallback() {
            @Override
            public void loadImages(HashMap<String, List<LocalImage>> images) {

                long totalStart = System.currentTimeMillis();
                long totalEnd = 0;
                int total = 0;
                if (images.size() == 0) {
                    totalEnd = System.currentTimeMillis();
                    postFinishMsg(images.size(), totalEnd - totalStart);
                    return;
                }

                for (Map.Entry<String, List<LocalImage>> entry : images.entrySet()) {
                    List<LocalImage> list = entry.getValue();

                    total = total + list.size();

                    for (LocalImage localImage : list) {


                        long start = System.currentTimeMillis();

                        postRecoMsg(localImage);


//                        Bitmap bitmap = BitmapHelper.compress(localImage.getAbsPath());

                        Bitmap bitmap = BitmapFactory.decodeFile(localImage.getAbsPath());

                        if (bitmap == null) {
                            continue;
                        }

                        //
                        double[] d = autoRecognitionHelper.recognize(bitmap);

                        bitmap.recycle();

                        long end = System.currentTimeMillis();

                        postResultMsg(localImage, d, end - start);
                        Logger.d(localImage);
                    }
                }

                totalEnd = System.currentTimeMillis();
                postFinishMsg(total, totalEnd - totalStart);

            }
        });

    }


    public void startDhash(Context context) {

        postStartMsg();
        ImageLocalDataSource localDataSource = new ImageLocalDataSource();
        localDataSource.loadLocalImages(context, new ImageDataSource.LoadImagesCallback() {
            @Override
            public void loadImages(HashMap<String, List<LocalImage>> images) {

                long totalStart = System.currentTimeMillis();
                long totalEnd = 0;
                int total = 0;
                if (images.size() == 0) {
                    totalEnd = System.currentTimeMillis();
                    postFinishMsg(images.size(), totalEnd - totalStart);
                    return;
                }

                for (Map.Entry<String, List<LocalImage>> entry : images.entrySet()) {
                    List<LocalImage> list = entry.getValue();

                    total = total + list.size();

                    for (LocalImage localImage : list) {
                        long start = System.currentTimeMillis();
                        postRecoMsg(localImage);

                        Bitmap bitmap = BitmapHelper.compress(localImage.getAbsPath());
                        double[] d = new double[2];
                        int hanD = DHash.hanDist(bitmap);
                        d[0] = (double) hanD;

                        bitmap.recycle();

                        long end = System.currentTimeMillis();
                        postResultMsg(localImage, d, end - start);
                    }
                }


                totalEnd = System.currentTimeMillis();

                postFinishMsg(total, totalEnd - totalStart);

            }
        });
    }


    private void demo() {

        AutoRecognitionHelper autoRecognitionHelper = AutoRecognitionHelper.getInstance();
        autoRecognitionHelper.is3D("图片的绝对路径");//单一文件识别调用
        //参数:图片id ，图片绝对路径 hashMap集合
        //返回结果：图片id,是否3D图片 hashMap集合
        HashMap<Long, Boolean> result = autoRecognitionHelper.recognize(new HashMap<Long, String>());//同步批量识别调用

        //参数:图片id ，图片绝对路径 hashMap集合
        //返回结果：图片id,是否3D图片 hashMap集合
        autoRecognitionHelper.asyncRecognize(new HashMap<Long, String>(), new AutoRecognitionHelper.OnAutoRecogniztionListener() {
            @Override
            public void result(HashMap<Long, Boolean> hashMap) {
                //结果返回
            }
        });//异步批量识别调用

    }

}
